import { ref } from "vue";

export const useFormRules = () => {
  // 校验规则
  const formRules = ref({
    username: [
      {
        required: true,
        whitespace: true,
        message: "用户名不能为空",
        trigger: "blur",
      },
      { min: 2, max: 20, message: "长度在2-20之间", trigger: "blur" },
      {
        pattern: /^[^\s]*$/,
        message: "禁止输入空格",
      },
    ],
    password: [
      {
        required: true,
        whitespace: true,
        message: "密码不能为空",
        trigger: "blur",
      },
      { min: 6, max: 20, message: "长度在6-20之间", trigger: "blur" },
      {
        pattern: /^[^\s]*$/,
        message: "禁止输入空格",
      },
    ],
    email: [
      {
        required: true,
        whitespace: true,
        message: "邮箱不能为空",
        trigger: "blur",
      },
      {
        pattern:
          /^[A-Za-zd0-9]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/,
        message: "邮箱格式错误",
        trigger: "blur",
      },
      {
        pattern: /^[^\s]*$/,
        message: "禁止输入空格",
      },
    ],
    mobile: [
      {
        required: true,
        whitespace: true,
        message: "手机号不能为空",
        trigger: "blur",
      },
      {
        pattern: /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/,
        message: "手机号格式错误",
        trigger: "blur",
      },
      {
        pattern: /^[^\s]*$/,
        message: "禁止输入空格",
      },
    ],
    role_id: [
      {
        required: true,
        whitespace: true,
        message: "角色不能为空",
        trigger: "blur",
      },
    ],
  });

  return {
    formRules,
  };
};
